package com.example.administrator.wallet.custom;

import android.util.Log;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.net.URISyntaxException;


/**
 * Created by Administrator on 2018/8/24 0024.
 */

public class MyWebSocketClient extends WebSocketClient {

    public static final String TAG = "MyWebSocketClient";

    private WebSocketStateChangeListener listener;
    private WebSocketStateChangeListener mSketchViewListener;
    private static MyWebSocketClient client;

    private int id;

    public MyWebSocketClient(URI serverUri) {
        super(serverUri);
    }

    public void setmSketchViewListener(WebSocketStateChangeListener mSketchViewListener) {
        this.mSketchViewListener = mSketchViewListener;
    }

    @Override
    public void onOpen(ServerHandshake handshakedata) {

        Log.i(TAG,"new connection opened");

        if(this.listener!=null){
            listener.onOpen(handshakedata);
        }
        if(this.mSketchViewListener!=null){
            mSketchViewListener.onOpen(handshakedata);
        }


    }

    @Override
    public void onMessage(String message) {

        Log.i(TAG,"received message: " + message);

        if(this.listener!=null){
            listener.onMessage(message);
        }

        if(this.mSketchViewListener!=null){
            mSketchViewListener.onMessage(message);
        }

    }

    @Override
    public void onClose(int code, String reason, boolean remote) {

        Log.i(TAG,"closed with exit code " + code + " additional info: " + reason);

        if(this.listener!=null){
            listener.onClose(code, reason,remote);
        }

        if(this.mSketchViewListener!=null){
            mSketchViewListener.onClose(code, reason,remote);
        }

    }

    @Override
    public void onError(Exception ex) {

        Log.i(TAG,"an error occurred:" + ex);

        if(this.listener!=null){
            listener.onError(ex);
        }

        if(this.mSketchViewListener!=null){
            mSketchViewListener.onError(ex);
        }

    }

    private int getId(){
        id++;
        return id;
    }

    public int sendMessage(String method, JSONArray jsonArray){
        int id = getId();
        JSONObject json = new JSONObject();
        json.put("id",id);
        json.put("method",method);
        json.put("params",jsonArray);
        if (client != null && client.isOpen()){
            this.send(json.toJSONString());
            Log.i(TAG,"send Message:" + json.toJSONString());
        }
        return id;
    }

    public static MyWebSocketClient initWebsocket(final String mWsUrl, final WebSocketStateChangeListener lis) {
        try {
            client = new MyWebSocketClient( new URI( mWsUrl ));
            client.listener = lis;
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                client.connect();
            }
        }).start();
        return client;
    }

    public JSONArray getJsonArray(String... args) {
        JSONArray jsonArray = new JSONArray();
        for (String arg: args) {
            jsonArray.add(arg);
        }
        return jsonArray;
    }

    public int join(String token) {
        if (client == null) {
            throw new RuntimeException("please invoke initWebsocket first");
        }
        /*JSONArray jsonArray = new JSONArray();
        jsonArray.add(token);
        jsonArray.add("android");*/
        JSONArray jsonArray = getJsonArray(token,"android");
        int id = sendMessage("server.auth", jsonArray);
        return id;
    }

}
